package file;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 文件复制
 */
public class FileCopeDemo {
    public static void main(String[] args) throws IOException {
        String file1 = "/Users/guopanbo/Documents/study/java/file/file1.pdf";
        String file2 = "/Users/guopanbo/Documents/study/java/file/file2.pdf";

        FileChannel readChannel = new FileInputStream(file1).getChannel(),
                writeChannel = new FileOutputStream(file2).getChannel();

        ByteBuffer buffer = ByteBuffer.allocateDirect(1024 * 1024);
        while((readChannel.read(buffer)) != -1) {
            buffer.flip();
            writeChannel.write(buffer);
            buffer.clear();
        }
        System.out.println("ok!");
    }
}
